package com.asjy.controller;

import cn.dsna.util.images.ValidateCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Auther: lilinlin
 * @Date: 2021/4/8
 * @Description: com.asjy.controller
 * @version: 1.0
 */
@Controller
@RequestMapping("/validate")
public class ValidateController {
    @Resource
    private HttpSession session;

    /**
     * 获取验证码
     * @param response
     * 返回 图片的字节流
     */
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response){

       try {
           //解决：浏览器缓存
           //设置浏览器端缓存信息
           /*response.setHeader("Cache-Control", "no-store");
           response.setHeader("Pragrma", "no-cache");
           response.setDateHeader("Expires", 0);*/

           ValidateCode validateCode = new ValidateCode(163,64,4,4);
           String code = validateCode.getCode();
           session.setAttribute("code",code);
           validateCode.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 验证验证码是否正确
     * @return   ok  no
     */
    @RequestMapping("/checkValildate")
    @ResponseBody
    public String checkValildate(String code){
        String sysCode = (String)session.getAttribute("code");
        if(code.equalsIgnoreCase(sysCode)){
            return "ok";
        }else{
            return "no";
        }
    }

}